package com.ruoyi.walmart.service.items.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.walmart.domain.Items.PairProducts;
import com.ruoyi.walmart.mapper.Items.PairProductsMapper;
import com.ruoyi.walmart.service.items.PairProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PairProductsServiceImpl implements PairProductsService {
    @Autowired
    PairProductsMapper pairProductsMapper;
    @Override
    public int insertAll(PairProducts pairProducts) {
        if(pairProducts.getProductId() == null){
            throw new ServiceException("未选择要配对的产品，配对失败!");
        }

        pairProducts.setCreateTime(DateUtils.getNowDate());

        String sellerSku = pairProducts.getItemSku(); // 拿到walmart的sku
        // 校验walmart链接是否已经被配对过了
        int result = pairProductsMapper.selectBySellerSku(sellerSku);

        if(result > 0){
            throw new ServiceException("本次配对的walmart链接已经被其他产品配对！");
        }
        return pairProductsMapper.insertAll(pairProducts);
    }

    @Override
    public int updateProductPairIdInt(Long[] pairIds, String head) {
        return pairProductsMapper.updateProductPairIdInt(pairIds,head);
    }


    @Override
    public int updateSalePairIdInt(Long[] pairIds, String head) {
        return pairProductsMapper.updateSalePairIdInt(pairIds,head);
    }


    @Override
    public int updatePairId(Long pairId,String nickName) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return pairProductsMapper.updatePairId(pairId,nickName,user.getUserId());
    }

    @Override
    public List<PairProducts> selectByPairId(Long[] pairIds) {
        return pairProductsMapper.selectByPairId(pairIds);
    }

    @Override
    public int updateProductIdInt(Long pairId, Long productId) {
        return pairProductsMapper.updateProductIdInt(pairId,productId);
    }

    @Override
    public int updateHead(Long pairId,String nickname) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return pairProductsMapper.updateHead(pairId,nickname,user.getUserId());
    }

    @Override
    public List<PairProducts> selectPairView(Long id, Date startTime, Date endTime) {
        return pairProductsMapper.selectPairView(id,startTime,endTime);
    }

    @Override
    public Integer selectUnpairedProductsStats() {
        return pairProductsMapper.selectUnpairedProductsStats();
    }
}
